<?php

namespace app\api\controller;

use app\common\controller\Api;
use app\api\services\Config as configService;
use think\Db;
use think\exception\HttpException;
use think\Log;

/**
 * 示例接口
 */
class Config extends Api
{

    //如果$noNeedLogin为空表示所有接口都需要登录才能请求
    //如果$noNeedRight为空表示所有接口都需要验证权限才能请求
    //如果接口已经设置无需登录,那也就无需鉴权了
    //
    // 无需登录的接口,*表示全部
    protected $noNeedLogin = ['*'];
    // 无需鉴权的接口,*表示全部
    protected $noNeedRight = ['*'];


    /**
     * 无需登录的接口
     *
     */
    public function getConfig(){
        $validate = new \think\Validate([
            'type'    =>'require',
        ]);
        $params = $this->request->get();
        if(!$validate->check($params)){
            $this->error($validate->getError());
        }
        $configService = new configService();
        $data = $configService->getTaskConfig($params);
        if(!$data){
            $this->error('fail', []);
        }
        $this->success('succ', $data);
    }

    /**
     * 获取任务配置列表
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getConfigList(){
        $params = $this->request->post();
        $validate = new \think\Validate([
            'id'    =>'integer',
        ]);
        if(!$validate->check($params)){
            $this->error($validate->getError());
        }
        $where['Status'] = '1';
        $str2 = 'Id,Name';
        $data = [];
        $data2 = [];
        if(isset($params['query_field']) && $params['query_field']){
            $str2 .= ', '.$params['query_field'];
        }
        if(isset($params['id']) && $params['id']){
            $where['Id'] = $params['id'];
            $data = Db::name('task_config')->field($str2)->where($where)->find();
            if(isset($data['params_type_id'])){
                $paramTypes = Db::name('params_type')->field('name')->where('id',$data['params_type_id'])->find();
                $data['params_type_name'] = $paramTypes['name'];
            }
            $this->success('succ', $data);
        }
        try {
            $data = Db::name('task_config')->field($str2)->where($where)->select();
        } catch (\Exception $e){
            $this->error('fail', []);
        }
        if($data){
            foreach($data as $k=>$v){
                $data2[$v['Id']] = $v;
            }
            $this->success('succ', $data2);
        }else{
            $this->error('fail', []);
        }

    }

    public function checkTaskConfig(){
        $params = $this->request->post();
        $configService = new configService();
        $data = $configService->checkTaskConfig($params);
        $this->success('succ', $data);
    }


    /**
     * 根据detail里面的key值,更新kuaipai任务名称
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     */
    public function update_config_name(){
        $config_data = Db::name('task_config')->where('Status','=',1)
            ->where('params_type_id','=',4)->select();
        $i = 0;
        foreach($config_data as $k=>$v){
            $detail_arr = json_decode($v['Detail'],true);
            $data['Name'] = $detail_arr['actions'][0]['operations'][1]['key'];
            $data['update_time'] = time();
            Db::name('task_config')->where('Id','=',$v['Id'])->update($data);
            $i += 1;
        }
        echo '更新完毕--数量是---'.$i.'个-----';
    }

    /**
     * 快排配置中，包含"exit"的json object中的sub-page->duration的值改成30~45之间的随机值
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     */
    public function update_duration(){
        $res_data = Db::name('task_config')->where('params_type_id','=',4)->select();
        $i = 0;
        foreach($res_data as $k=>$v){
            $detail_arr = json_decode($v['Detail'],true);
            foreach($detail_arr['actions'][0]['operations'] as $kk=>&$vv){
                if(isset($vv['exit'])){
                    $vv['sub-page']['duration'] = rand(31,44);
                }
            }
            $Detail = json_encode($detail_arr);
            Db::name('task_config')->where('Id','=',$v['Id'])->update(['Detail'=>$Detail]);
            $i += 1;
        }
        echo 'success--------'.$i;
    }






}
